package com.sun.interceptor;

import com.sun.utils.SystemConstant;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        // 判断session中有无登录用户
        if (session.getAttribute(SystemConstant.LOGINUSER) == null) {
            // 如果未登录则跳转到登录界面
            response.sendRedirect(request.getContextPath() + "/admin/login.html");
            return false;
        }
        // 如果登录则放行
        return true;
    }
}
